home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 5 / WINDOW_F / WINDOW_F.C < prev   
C/C++ Source or Header  |  1992-07-19  |  4KB  |  170 lines

  1. /*
  2.     Window Font MDEF by James W. Walker, Nov. 1991, updated April 1992.
  3.     76367.2271@compuserve.com
  4.     
  5.     This MDEF uses the font and size from the front window,
  6.     rather than the system font, but otherwise calls the
  7.     standard MDEF to do most of the work.  It also redraws the
  8.     triangular scrolling indicators, which otherwise can get scaled
  9.     with an ugly result.
  10. */
  11.  
  12. #include "Draw_arrow.h"
  13.  
  14.  
  15. #if ASSERTING
  16.     #define        ASSERT(cond,mess)    if (!(cond)) DebugStr("\p" mess)
  17. #else
  18.     #define        ASSERT(cond,mess)
  19. #endif
  20.  
  21. #if TRACE
  22.     #define     TM(what)    DebugStr( "\p" #what )
  23. #else
  24.     #define        TM(what)
  25. #endif
  26.  
  27. typedef pascal void (*Menu_def) (short, MenuHandle, Rect *, Point, short *);
  28.  
  29.  
  30. pascal void main(
  31.     short        message,
  32.     MenuHandle    theMenu,
  33.     Rect        *menuRect,
  34.     Point        hitPt,
  35.     short        *whichItem
  36.     );
  37.  
  38. /*  Low-memory globals */
  39. short    SysFontFam    : 0xBA6;
  40. short    SysFontSize    : 0xBA8;
  41. short    LastSpExtra    : 0xB4C;
  42. short    CurFMInput    : 0x988;
  43.  
  44. /* global variables (A4-relative addressing) */
  45. Boolean        has_up_arrow, has_down_arrow;
  46. short        line_height;
  47.  
  48. /* -------------------------------------------------------------------- */
  49. pascal void main(
  50.     short        message,
  51.     MenuHandle    theMenu,
  52.     Rect        *menuRect,
  53.     Point        hitPt,
  54.     short        *whichItem
  55.     )
  56. {
  57.     Menu_def    old_menudef;
  58.     Handle        standard_MDEF_rsrc;
  59.     SignedByte    menu_state;
  60.     GrafPtr        wmgr_port, port;
  61.     short        save_size;
  62.     short        save_sysfam;
  63.     FontInfo    fi;
  64.     long        save_A4;
  65.     Boolean        had_up_arrow, had_down_arrow;
  66.     
  67.     RomMapInsert = -1;
  68.     standard_MDEF_rsrc = GetResource( 'MDEF', 0 );
  69.     ASSERT( standard_MDEF_rsrc != NIL, "No MDEF" );
  70.     menu_state = HGetState( standard_MDEF_rsrc );
  71.     HLock( standard_MDEF_rsrc );
  72.     old_menudef = (Menu_def) StripAddress( *standard_MDEF_rsrc );
  73.  
  74.     GetPort( &wmgr_port );
  75.     save_size = wmgr_port->txSize;
  76.     save_sysfam = SysFontFam;
  77.     
  78.     port = FrontWindow();
  79.  
  80.     /*
  81.         Since the standard MDEF calls TextFont(0), we can't just set
  82.         the font of the current port (the Window Manager port).  We
  83.         must set the system font instead. On the other hand the standard
  84.         MDEF does not call TextSize(), so we can set the text size in
  85.         the port.
  86.     */
  87.     SysFontFam = port->txFont;
  88.     LastSpExtra = -1;
  89.     
  90.     /*
  91.         If we change the font but not the size, the Font Manager sometimes
  92.         seems to need an extra kick in the butt to notice the font change.
  93.         This seems to do it.  Witchcraft.
  94.         
  95.         Setting CurFMInput to -1 may also work, except that I have not
  96.         seen any documentation on this global.
  97.     */
  98.     TextSize( 3 );
  99.     GetFontInfo( &fi );
  100.  
  101.     TextSize( port->txSize );
  102.  
  103.     old_menudef( message, theMenu, menuRect, hitPt, whichItem );
  104.  
  105.     asm {
  106.         move.L        A4, save_A4
  107.         LEA            main, A4        ; set up access to global variables
  108.     }
  109.  
  110.     HSetState( standard_MDEF_rsrc, menu_state );
  111.     
  112.     /*
  113.         The purpose of this switch group is to fix a cosmetic problem
  114.         with the scrolling triangles that sometimes appear at the top
  115.         or bottom of the menu.  Normally these are drawn by the standard
  116.         MDEF using a CopyMask call.  When a different font size is used,
  117.         this bitmap gets scaled, with an ugly result.  So we erase and
  118.         redraw the scrolling triangle.
  119.     */
  120.     
  121.     switch (message)
  122.     {
  123.         case mChooseMsg:
  124.             TM(choose);
  125.             had_up_arrow = has_up_arrow;
  126.             has_up_arrow = (TopMenuItem < menuRect->top);
  127.             had_down_arrow = has_down_arrow;
  128.             has_down_arrow = (AtMenuBottom > menuRect->bottom);
  129.             if ( !had_up_arrow && has_up_arrow )
  130.             {
  131.                 Draw_arrow( menuRect, -1 );
  132.             }
  133.             if ( !had_down_arrow && has_down_arrow )
  134.             {
  135.                 Draw_arrow( menuRect, 1 );
  136.             }
  137.             break;
  138.         case mDrawMsg:
  139.             // SetPort( port );
  140.             GetFontInfo( &fi );
  141.             // SetPort( wmgr_port );
  142.             line_height = fi.ascent + fi.descent + fi.leading;
  143.             has_up_arrow = TopMenuItem < menuRect->top;
  144.             has_down_arrow = AtMenuBottom > menuRect->bottom;
  145.             if (has_up_arrow)
  146.             {
  147.                 Draw_arrow( menuRect, -1 );
  148.             }
  149.             if (has_down_arrow)
  150.             {
  151.                 Draw_arrow( menuRect, 1 );
  152.             }
  153.             break;
  154.     }
  155.  
  156.     /*
  157.         Restore the font and size.
  158.     */
  159.     SysFontFam = save_sysfam;
  160.     LastSpExtra = -1;
  161.  
  162.     TextSize( 3 );            // again, kick the font manager
  163.     GetFontInfo( &fi );
  164.  
  165.     TextSize( save_size );
  166.     
  167.     asm {
  168.         move.L        save_A4, A4
  169.     }
  170. }